/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.knowledge.weirdness;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import co.edu.unal.bioingenium.kbmed.text.filter.FilterPipe;
import co.edu.unal.bioingenium.kbmed.text.representation.io.CorpusIO;
import co.edu.unal.bioingenium.kbmed.text.representation.vo.Corpus;
import co.edu.unal.bioingenium.kbmed.util.io.FileManager;
import gnu.trove.map.TObjectFloatMap;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectFloatHashMap;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ing. Alejandro Riveros Cruz
 */
public class CalculateWeirdness {

    private TObjectFloatMap<String> generalDictionarty;
    private int totalGeneralDictionarty = 152558294;
    private TObjectFloatMap<String> specializedDictionarty;
    private int totalSpecializedDictionarty;
    String s = "";

    /**
     * 
     */
    public CalculateWeirdness() {
        generalDictionarty = new TObjectFloatHashMap<String>();
        specializedDictionarty = new TObjectFloatHashMap<String>();
    }

    /**
     * 
     * @param RAEDictionaryName
     * @param corpusName
     */
    public void loadDictionaries(String RAEDictionaryName, String corpusName) {
        FilterPipe filterPipe = new FilterPipe();
        filterPipe.setMinTokenLength(1);
        filterPipe.setMaxTokenLength(20);
        filterPipe.setRemoveStopWords(true);
        filterPipe.setRemoveNumbers(true);
        String word2;
        try {
            //General Dictionary
            BufferedReader bufferedReader = new BufferedReader(new FileReader(Configuration.DATA_DIR + File.separator + RAEDictionaryName + ".txt"));
            String line;
            String[] tokens;
            while ((line = bufferedReader.readLine()) != null) {
                tokens = line.split(",");
                word2 = filterPipe.singleFilter(tokens[0]);
                if (tokens[0].equalsIgnoreCase(word2) && tokens[0].length() > 1) {
                    generalDictionarty.put(tokens[0], Float.parseFloat(tokens[1]));
                }
            }
            bufferedReader.close();
        } catch (IOException ex) {
            Logger.getLogger(CalculateWeirdness.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Specializaed Dictionary
        Corpus corpus = CorpusIO.loadCorpus(corpusName + ".dat");
        TObjectIntMap<String> wordList = corpus.getWordCount();
        totalSpecializedDictionarty = 0;
        for (String word : wordList.keySet()) {
            totalSpecializedDictionarty += wordList.get(word);
        }
        for (String word : wordList.keySet()) {
            word2 = filterPipe.singleFilter(word);
            if (word.equalsIgnoreCase(word2) && word.length() > 1) {
                specializedDictionarty.put(word, (float) wordList.get(word) / (float) totalSpecializedDictionarty);
            }
        }
    }

    /**
     * 
     * @param RAEDictionaryName
     * @param corpusName
     */
    public void saveWeirdness(String RAEDictionaryName, String corpusName) {
        loadDictionaries(RAEDictionaryName, corpusName);
        TObjectFloatMap<String> weirdness = new TObjectFloatHashMap<String>();
        for (String word : specializedDictionarty.keySet()) {
            if (generalDictionarty.containsKey(word)) {
                weirdness.put(word, specializedDictionarty.get(word) / generalDictionarty.get(word));
            } else {
                weirdness.put(word, specializedDictionarty.get(word) / ((float) 1 / (float) totalGeneralDictionarty));
            }
        }
        BufferedWriter bufferedWriter = null;
        try {
            String savePath = Configuration.DATA_DIR + File.separator + CalculateWeirdness.class.getSimpleName();
            FileManager.verifyAndCreatePath(savePath);
            bufferedWriter = new BufferedWriter(new FileWriter(new File(Configuration.DATA_DIR + File.separator + corpusName + "-" + RAEDictionaryName + "ConceptWeirdness.csv")));
            for (String word : weirdness.keySet()) {
                bufferedWriter.write(word + "," + weirdness.get(word) + "\n");
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(CalculateWeirdness.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(CalculateWeirdness.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        CalculateWeirdness calculateWeirdness = new CalculateWeirdness();
        calculateWeirdness.saveWeirdness("RAE-20000words", "conceptAllTerms");

    }
}
